package com.alili.springbooteditor.controller;

import com.alili.springbooteditor.dao.MessagesDao;
import com.alili.springbooteditor.entities.comments;
import com.alili.springbooteditor.entities.editors;
import com.alili.springbooteditor.entities.messages;
import com.alili.springbooteditor.repository.MessagesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class messagesController {
    @Autowired
    MessagesRepository messagesRepository;
    @Autowired
    MessagesDao messagesDao;
    //来到留言页面显示留言
    @GetMapping("/gustbook")
    public String toAndShowMessages(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum,
                                    ModelMap modelMap, ModelMap modelMap2){
        PageRequest pageable = PageRequest.of(pageNum, 3, Sort.Direction.DESC, "messageDate");
        Page<messages> p = messagesRepository.findAll(pageable);//按照日期排序显示
        List<messages> allMessages = p.getContent();
        modelMap.addAttribute("pages",p);
        modelMap2.addAttribute("messages",allMessages);

        return "gustbook";
    }
    //提交留言
    @PostMapping("/upMessage")
    public String addMessages(messages message){
        messagesDao.save(message);
        return "redirect:/gustbook";
    }

}
